// pages/offerDetails/offerDetails.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    like: true,
    likeIcon: "../../images/index/likebtnoff.png",
    collect: true,
    collectIcon: "../../images/index/collectoff.png",
    offers : "",
    standerSorce : "",
    comments : "",
    userInfo : "",
    otherDesc_content:true,
    auto_focus: false,
    auto_focus2:false,
    msgboard_content:true,
    showBottom:true,//显示底部
    showInput:false,//显示发布输入框
    comValue: '',
    getInput: '',
    currentOffer: {},
    oldLeaveMsg:'',
    oldOtherInstructions:'',
    scores:{}
  },
  // 我有话说
  writeComment:function(){
    let that = this;
    that.setData({
      showBottom: false,
      showInput: true
    })
  },
  // 关闭发布
  closeInput:function(){
    this.setData({
      showBottom: true,
      showInput: false
    })
  },
  // 发布输入框
  getInput: function (e) {
    this.setData({
      comValue: e.detail.value
    })
  },
  // 发布
  sendComment:function(){
    let getComment = this.data.comValue;
    // 发布成功
    this.setData({
      showBottom: true,
      showInput: false
    })
    //请求接口
    var userId = wx.getStorageSync('userInfo').id //获取用户id
    var userName = wx.getStorageSync('userInfo').nickName //获取用户id
    let offerInterviewComment = {}
    offerInterviewComment.commentatorId = userId
    offerInterviewComment.commentatorName = userName
    offerInterviewComment.beCommentatorId = this.data.userInfo.id
    offerInterviewComment.beCommentatorName = this.data.userInfo.nickName
    offerInterviewComment.offerInterviewId = this.data.currentOffer.id
    offerInterviewComment.comments = getComment
    offerInterviewComment.commentType = 1 //1offer2面经
    util.request(api.OfferIntevieCommentAdd, offerInterviewComment, 'POST', true).then(function (res) {
      console.log(res)
    })
  },
  // 跳转到修改标化成绩
  goToEdit:function(){
    var standerSorce = this.data.standerSorce;
    console.log(standerSorce);
    if(this.data.standerSorce){
      wx.navigateTo({
        url: '../../pages/editScores/editScores?standerSorce='+JSON.stringify(standerSorce),
      })
    }
  },
  // 跳转到修改offer
  goToEditOffer: function (event) {
    var index = event.currentTarget.id;
    console.log(index);
    var offers = this.data.offers;
    console.log(offers)
    console.log(offers.length)
    if(offers && offers.length > index){
      var navigateData = offers[index];
      console.log(navigateData)
      wx.navigateTo({
        url: '../../pages/editOffer/editOffer?offer='+JSON.stringify(navigateData),
      })
    }
  },
  // 点击其他说明修改按钮
  editotherDesc_content:function(e){
    let isEdit = this.data.otherDesc_content;
    let auto_focus = this.data.auto_focus;
    if(isEdit){
      this.setData({
        otherDesc_content: !isEdit,
        auto_focus: !auto_focus,
        oldOtherInstructions: this.data.standerSorce.otherInstructions
      })
    }else{
      var standerSorce = this.data.standerSorce;
      standerSorce.otherInstructions = this.data.oldOtherInstructions;
      this.setData({
        otherDesc_content: !isEdit,
        auto_focus: !auto_focus,
        standerSorce: standerSorce
      })
    }
  },
  // 点击留言修改按钮
  editmsgboard_content:function(e){
    let isEdit = this.data.msgboard_content;
    let auto_focus2 = this.data.auto_focus2;
    if(isEdit){
      this.setData({
        msgboard_content: !isEdit,
        auto_focus2: !auto_focus2,
        oldLeaveMsg: this.data.standerSorce.leaveMessage
      })
    }else{
      var standerSorce = this.data.standerSorce;
      standerSorce.leaveMessage = this.data.oldLeaveMsg;
      this.setData({
        msgboard_content: !isEdit,
        auto_focus2: !auto_focus2,
        standerSorce: standerSorce
      })
    }

  },
  // 保存其他说明
  saveOtherVal:function(e){
    let val = this.data.standerSorce;
    let isEdit = this.data.otherDesc_content;
    let auto_focus = this.data.auto_focus;
    var standerSorce = {
      id : this.data.standerSorce.id,
      otherInstructions : this.data.standerSorce.otherInstructions
    }
    var that = this;
    util.request(api.StanderSorceUpdate, JSON.stringify(standerSorce), "POST", true).then(function (res) {
      if (res.code === 200) {
        wx.showToast({
          title: '修改其他说明成功!',
          icon: 'none',
          duration: 1500
        })
        that.setData({
          otherDesc_content: !isEdit,
          auto_focus: !auto_focus
        })
      }else{
        wx.showToast({
          title: '修改其他说明失败!',
          icon: 'none',
          duration: 1500
        })
        var standerSorce = that.data.standerSorce;
        standerSorce.otherInstructions = that.data.oldOtherInstructions;
        that.setData({
          otherDesc_content: !isEdit,
          auto_focus: !auto_focus,
          standerSorce: standerSorce
        })
      }
    })
  },
  // 保存留言
  saveMsgVal: function (e) {
    let val = this.data.standerSorce;
    let isEdit = this.data.msgboard_content;
    let auto_focus2 = this.data.auto_focus2;
    var standerSorce = {
      id : this.data.standerSorce.id,
      leaveMessage : this.data.standerSorce.leaveMessage
    }
    var that = this;
    util.request(api.StanderSorceUpdate, JSON.stringify(standerSorce), "POST", true).then(function (res) {
      if (res.code === 200) {
        wx.showToast({
          title: '修改留言成功!',
          icon: 'none',
          duration: 1500
        })
        that.setData({
          msgboard_content: !isEdit,
          auto_focus2: !auto_focus2
        })
      }else{
        wx.showToast({
          title: '修改留言失败!',
          icon: 'none',
          duration: 1500
        })
        var standerSorce = that.data.standerSorce;
        standerSorce.leaveMessage = that.data.oldLeaveMsg;
        that.setData({
          msgboard_content: !isEdit,
          auto_focus2: !auto_focus2,
          standerSorce: standerSorce
        })
      }
    })
  },
  // 其他说明文本框输入
  saveeditotherDesc_content:function(e){
    let standerSorce = this.data.standerSorce;
    let val = e.detail.value;
    let otherInstructions = `standerSorce.otherInstructions`;
    this.setData({
      [otherInstructions]: val
    })
    console.log(this.data.standerSorce)
  },
  //留言文本框输入
  saveleaveMessage: function (e) {
    let standerSorce = this.data.standerSorce;
    let val = e.detail.value;
    let leaveMessage = `standerSorce.leaveMessage`;
    this.setData({
      [leaveMessage]: val
    })
  },
  clickLike: function (e) {
    let isLike = this.data.like;
    if (isLike) {
      this.setData({
        likeIcon: "../../images/index/likebtnon.png",
        like: !isLike
      })
    } else {
      this.setData({
        likeIcon: "../../images/index/likebtnoff.png",
        like: !isLike
      })
    }
  },
  clickCollect: function () {
    let isCollect = this.data.collect;
    if (isCollect) {
      this.setData({
        collectIcon: "../../images/index/collecton.png",
        collect: !isCollect
      })
    } else {
      this.setData({
        collectIcon: "../../images/index/collectoff.png",
        collect: !isCollect
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var userId = options.beCommentatorId;
    if(userId || userId == null || userId == ''){
      userId = wx.getStorageSync('userInfo').id;
    }
    var offerId = options.offerInterviewId;
    var that = this;
    util.request(api.OfferDetail + "?userId=" + userId + "&offerId=" + offerId, null, 'GET', true).then(function(res) {
      console.log(res)
      if(res.code === 200){
        that.setData({
          offers : res.result.offer.offers,
          comments : res.result.comments,
          standerSorce : res.result.offer.standerSorce,
          userInfo : res.result.offer.tuser,
          currentOffer: res.result.offer.offer,
          scores : res.result.offer.standerSorce.scores == null ? {} : JSON.parse(res.result.offer.standerSorce.scores)
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var offerId = wx.getStorageSync('offerId');
    wx.removeStorageSync('offerId');
    var userId = "";
    if(userId || userId == null || userId == ''){
      userId = wx.getStorageSync('userInfo').id;
    }
    if(offerId && userId){
      console.log(111)
      var that = this;
      util.request(api.OfferDetail + "?userId=" + userId + "&offerId=" + offerId, null, 'GET', true).then(function(res) {
        console.log(res)
        if(res.code === 200){
          console.log(res.result.offer.offer);
          that.setData({
            offers : res.result.offer.offers,
            comments : res.result.comments,
            standerSorce : res.result.offer.standerSorce,
            userInfo : res.result.offer.tuser,
            currentOffer: res.result.offer.offer,
          })
        }
      });
    }
    var flag = wx.getStorageSync('isReloadStanderSorce');
    wx.removeStorageSync('isReloadStanderSorce');
    console.log(flag)
    if(userId && flag){
      console.log(111)
      console.log(this.data.currentOffer.id)
      console.log(this.data.currentOffer)
      var that = this;
      util.request(api.StanderSorceQuery + "?userId=" + userId + "&offerId=" + this.data.currentOffer.id, null, 'GET', true).then(function(res) {
        console.log(res)
        if(res.code === 200){
          that.setData({
            standerSorce : res.result[0],
          })
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})